home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0015_More Graphic Circles.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  3KB  |  110 lines

  1. {
  2. MICHAEL NICOLAI
  3.  
  4. >does someone have a circle routine For the 320x200x256 mode.
  5. >I need one using the Assembler...  (FAST) ( or isn't that possible)
  6. >I doesn't need to be very perfect, if it has the shape of a circle,
  7. >I'm satisfied.
  8.  
  9. I don't have any Asm-Program yet but i got the same question some time ago.
  10.  
  11. Ok then, let's do some math:
  12.  
  13. The basic formula (and quickest?) For drawing a circle is: x^2 + y^2 = r^2.
  14. The r stands For radius (half the diameter). You know this formula, i am
  15. sure. A guy called Phytagoras set i up very long ago to calculate the
  16. hypotenuse of a given triangle.
  17.  
  18.                    |\
  19.                    | \
  20.                  a |  \ c      c^2 = a^2 + b^2
  21.                    |   \
  22.                    |____\
  23.  
  24.                      b
  25. Remember?
  26.  
  27. Now look at this:        ...|     a quater of the circle
  28.                        ..   |
  29.                       . ____|y
  30.                      . |\   |
  31.                     .  | \  |
  32.                     .  | r\ |
  33.                     .  |   \|
  34.                --------------------------
  35.                     r  x    |0
  36.                             |
  37.                             |
  38.  
  39. r is given and take 0 - r as a starting point For x. Then all you have to do
  40. is to calculate y and plot the point.
  41.  
  42.     y = sqrt((r * r) - (x * x))      sqrt : square root
  43.  
  44. After each calculation x is increased Until it has reached 0. Then one
  45. quarter of the circle is drawn. The other three quarters are symmetrical.
  46.  
  47. I have written a short Program For you to draw a circle in 320x200x256
  48. Graphics mode. When you key in some values please remember that NO error
  49. checking will be done. x has to be between 0 and 319, and y between 0 and
  50. 199. The radius must not be greater than x and y.
  51.  
  52. Example: x : 160; y : 100; r : 90
  53.  
  54. When you start this Program you will not get correct circles because in
  55. Graphics mode ONE pixel is not square!!! You have to calculate an aspect
  56. ratio to get nice looking circles.
  57. }
  58.  
  59. Program circle;
  60.  
  61. Uses
  62.   Crt, Dos;
  63.  
  64. Var
  65.   regs    : Registers;
  66.   x0, y0  : Word;
  67.   x, y, R : Real;
  68.   temp    : Real;
  69.   c       : Char;
  70.  
  71. Procedure putpixel(x, y : Word; color : Byte);
  72. begin
  73.   mem[$A000 : (y * 320 + x)] := color;
  74. end;
  75.  
  76. begin
  77.  ClrScr;
  78.  Writeln('Enter coordinates of middle-point :');
  79.  Writeln;
  80.  Write('x : ');
  81.  readln(x0);
  82.  Write('y : ');
  83.  readln(y0);
  84.  Writeln;
  85.  Write('Enter radius :');
  86.  readln(R);
  87.  
  88.  { Switch to 320x200x256 }
  89.  
  90.  regs.ax := $0013;
  91.  intr($10, regs);
  92.  
  93.  x := (-1) * R;  { go from 0 - R to 0 }
  94.  temp := R * R;
  95.  Repeat
  96.    y := sqrt(temp - (x * x));
  97.    putpixel((x0 + trunc(x)), (y0 - trunc(y)), 15); { 4.th quadrant }
  98.    putpixel((x0 - trunc(x)), (y0 - trunc(y)), 15); { 1.st quadrant }
  99.    putpixel((x0 + trunc(x)), (y0 + trunc(y)), 15); { 3.rd quadrant }
  100.    putpixel((x0 - trunc(x)), (y0 + trunc(y)), 15); { 2.nd quadrant }
  101.    x := x + 0.1; { change this if you want coarse or fine circle. }
  102.  Until (x >= 0.0);
  103.  c := ReadKey;  { wait For keypress. }
  104.  
  105.  { Switch back to Textmode. }
  106.  
  107.  regs.ax := $0003;
  108.  intr($10, regs);
  109. end.
  110.